﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;
using Newtonsoft.Json;
using WebApplication1.classes.Core.JSTree;
using WebApplication1.classes.Core.Layout;
using WebApplication1.classes.dataSources;

namespace WebApplication1.services {
    [WebService( Namespace = "http://tempuri.org/" )]
    [ScriptService]
    [Serializable]
    public class LayoutService : WebService {

        const string WSLayoutJSonFilePath = "N:\\hand made\\dotNET\\Projects\\WebApplication1\\WebApplication1\\data\\layoutStorage.json";

        private static LayoutDatasource layoutDatasource_ = null;

        public LayoutService() {
            if ( layoutDatasource_ != null ) return;

            layoutDatasource_ = new LayoutDatasource();
            layoutDatasource_.filePath = WSLayoutJSonFilePath;
            layoutDatasource_.deserialize();
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public IList<Layout> layoutChildListForItemPath( string itemPath) {
            return layoutDatasource_.layoutChildListForItemPath(itemPath);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public string layoutJsTreeForItemPath(string itemPath) {
            return layoutDatasource_.layoutJsTreeForItemPath(itemPath);
        } 

        
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public bool deleteLayoutItemPath(string itemPath){
            return layoutDatasource_.deleteItemPath(itemPath);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public bool insertItemAtPosition(string parentPath, string itemName, int position){
            layoutDatasource_.insertItemAtPosition(parentPath, itemName, position);
            return true;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public bool renameItemForPath(string itemPath, string newName){
            layoutDatasource_.renameItem(itemPath,newName);
            return true;
        }
    }
}